package com.ssyc.gsk_teacher.activity;

import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;

import com.ssyc.common.base.BaseActivity;
import com.ssyc.gsk_teacher.R;
import com.ssyc.gsk_teacher.adapter.MusicAdapter;
import com.ssyc.gsk_teacher.bean.MusicInfo;

import java.util.ArrayList;
import java.util.List;

/**
 * 教师端--小组音乐选择界面
 * Created by Administrator on 2018/2/27 0027.
 */

public class TeacherChooseMusicAcitivity extends BaseActivity implements View.OnClickListener {

    //选择音乐后的结果码
    public static final int TEAMMUSIC = 0x240;

    private ImageView ivBack;

    private ListView lv;

    //数据集合
    private List<MusicInfo> infos;
    //适配器对象
    private MusicAdapter adapter;

    //播放音乐的资源集合
    private int[] musicId = {R.raw.teacher_music1, R.raw.teacher_music2, R.raw.teacher_music3, R.raw.teacher_music4, R.raw.teacher_music5, R.raw.teacher_music6, R.raw.teacher_music7, R.raw.teacher_music8, R.raw.teacher_music9, R.raw.teacher_music10};

    @Override
    protected boolean isNeedShowTitle() {
        return false;
    }

    @Override
    protected String setTitleText() {
        return "";
    }

    @Override
    protected int getContentLayout() {
        return R.layout.teacher_activity_choose_music;
    }

    @Override
    protected void init() {
        initView();
        showContent();
        initData();
        setLv();
    }

    /**
     * 初始化视图
     */
    public void initView() {
        lv = findViewById(R.id.lv);
        ivBack = findViewById(R.id.iv_back);
        ivBack.setOnClickListener(this);
    }

    /**
     * 初始化播放音乐的资源文件
     */
    private void initData() {
        infos = new ArrayList<>();
        for (int i = 1; i <= musicId.length; i++) {
            MusicInfo info = new MusicInfo();
            info.setName("小 组 音 乐 " + i);
            info.setResId(musicId[i - 1]);
            info.setPlaying(false);
            infos.add(info);
        }
    }


    /**
     * 设置适配器的方法
     */
    private void setLv() {
        adapter = new MusicAdapter(this, infos, R.layout.teacher_item_lv_music);
        lv.setAdapter(adapter);
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int pos, long l) {
                //条目的点击事件
                Intent intent = new Intent();
                intent.putExtra("index", (pos + 1) + "");
                setResult(TEAMMUSIC, intent);
                finish();
            }
        });
    }


    /**
     * 在onDestroy()方法中释放播放器资源
     */
    @Override
    protected void onDestroy() {
        if (adapter != null) {
            adapter.releaseMediaPlayer();
        }
        super.onDestroy();
    }


    /**
     * 点击事件
     *
     * @param view
     */
    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.iv_back) {
            //返回
            finish();
        }
    }
}
